/*
 * BQ24075.c
 *
 * Created: 13.01.2012 16:42:06
 *  Author: Alex
 */ 

#include <avr/io.h>
#include "BQ24075.h"

/* Batterielader BQ ansteuern */


void initCharge(void)
{
	// Erst auf High, da alle Low-Active
	BQ_PORT |= (1<<BQ_PIN_EN1)|(1<<BQ_PIN_EN2)|(1<<BQ_PIN_CE);
	BQ_DDR |= (1<<BQ_PIN_EN1)|(1<<BQ_PIN_EN2)|(1<<BQ_PIN_CE);
}


void setCharge(uint8_t current)
{
	if(current == CHARGE_100mA)
	{
		BQ_PORT &= ~((1<<BQ_PIN_EN1)|(1<<BQ_PIN_EN2)|(1<<BQ_PIN_CE));
		//BQ_PORT |= (1<<BQ_PIN_CE);
	}
	else if (current == CHARGE_500mA)
	{
		BQ_PORT &= ~((1<<BQ_PIN_EN2)|(1<<BQ_PIN_CE));
		BQ_PORT |= (1<<BQ_PIN_EN1);
	}
	else if (current == CHARGE_ILIM)
	{
		BQ_PORT &= ~((1<<BQ_PIN_EN1)|(1<<BQ_PIN_CE));
		BQ_PORT |= (1<<BQ_PIN_EN2);
	}
	else
	{
		BQ_PORT |= (1<<BQ_PIN_EN1)|(1<<BQ_PIN_EN2);
		BQ_PORT |= (1<<BQ_PIN_CE);
	}
}

void setPower(uint8_t on)
{
	if(on == 0)
	{
		
		PORTD &= ~(1<<PIND7);
		DDRD &= ~(1<<PIND7);
		setCharge(0);
	}
	else
	{
		DDRD |= (1<<PIND7);
		PORTD |= (1<<PIND7);
	}
}